package com.vm.activity;

import java.io.File;
import com.vm.commons.CurrentSetting;
import com.vm.db.SettingDao;
import com.wwtscapp.apk.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月10日 下午6:06:06]
 * @Version: [v1.0]
 * 
 */
public class SettingActivity extends Activity {
	ImageView imgbtn_search_back;
	ImageView switch_no_pic;
	ImageView switch_wifi_only;
	ImageView switch_zero_flow;
	ImageView switch_install_after_downloaded;
	ImageView switch_delete_apk_after_downloaded;
	ImageView switch_root_quick_install;
	ImageView switch_app_update_notification;
	ImageView switch_screen_auto_rotation;
	ImageView down_complete_sound;
	SettingDao sDao;
	RelativeLayout setting_package_storage_location;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_up);
		sDao=new SettingDao(this);
		init();
	}

	public void init() {
		setting_package_storage_location=(RelativeLayout)findViewById(R.id.setting_package_storage_location);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		switch_no_pic = (ImageView) findViewById(R.id.switch_no_pic);
		switch_wifi_only = (ImageView) findViewById(R.id.switch_wifi_only);
		switch_install_after_downloaded = (ImageView) findViewById(R.id.switch_install_after_downloaded);
		switch_delete_apk_after_downloaded = (ImageView) findViewById(R.id.switch_delete_apk_after_downloaded);
		switch_app_update_notification = (ImageView) findViewById(R.id.switch_app_update_notification);
		down_complete_sound=(ImageView)findViewById(R.id.down_complete_sound);
		switch_no_pic.setOnClickListener(Btn_Listener);
		switch_wifi_only.setOnClickListener(Btn_Listener);
		switch_install_after_downloaded.setOnClickListener(Btn_Listener);
		switch_delete_apk_after_downloaded.setOnClickListener(Btn_Listener);
		switch_app_update_notification.setOnClickListener(Btn_Listener);
		setting_package_storage_location.setOnClickListener(Btn_Listener);
		down_complete_sound.setOnClickListener(Btn_Listener);
		if(CurrentSetting.nopic == 0){
			switch_no_pic
			.setBackgroundResource(R.drawable.settings_switch_off);
		}else{
			switch_no_pic
			.setBackgroundResource(R.drawable.settings_switch_on);
		}
		if(CurrentSetting.onlywifi == 0){
			switch_wifi_only
			.setBackgroundResource(R.drawable.settings_switch_off);
		}else{
			switch_wifi_only
			.setBackgroundResource(R.drawable.settings_switch_on);
		}
		if(CurrentSetting.autoinstall == 0){
			switch_install_after_downloaded
			.setBackgroundResource(R.drawable.settings_switch_off);
		}else{
			switch_install_after_downloaded
			.setBackgroundResource(R.drawable.settings_switch_on);
		}
		if(CurrentSetting.autodelete == 0){
			switch_delete_apk_after_downloaded
			.setBackgroundResource(R.drawable.settings_switch_off);
		}else{
			switch_delete_apk_after_downloaded
			.setBackgroundResource(R.drawable.settings_switch_on);
		}
		if(CurrentSetting.updatenotification == 0){
			switch_app_update_notification
			.setBackgroundResource(R.drawable.settings_switch_off);
		}else{
			switch_app_update_notification
			.setBackgroundResource(R.drawable.settings_switch_on);
		}
		if(CurrentSetting.zeroflow==0){
			down_complete_sound.setBackgroundResource(R.drawable.settings_switch_off);
		}else{
			down_complete_sound.setBackgroundResource(R.drawable.settings_switch_on);
		}
	}

	OnClickListener Btn_Listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.down_complete_sound:
				if(CurrentSetting.zeroflow==0){
					down_complete_sound.setBackgroundResource(R.drawable.settings_switch_on);
					CurrentSetting.zeroflow=1;
					sDao.update3(1);
				}else{
					down_complete_sound.setBackgroundResource(R.drawable.settings_switch_off);
					CurrentSetting.zeroflow=0;
					sDao.update3(0);
				}
				break;
			case R.id.setting_package_storage_location:
				Intent intent = new Intent();
				intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
				File file=new File(Environment.getExternalStorageDirectory().getPath()+"/vmclient");
				intent.setDataAndType(Uri.fromFile(file),"*/*");
				startActivity(intent);
				break;
			case R.id.switch_no_pic:
				if (CurrentSetting.nopic == 0) {
					switch_no_pic
							.setBackgroundResource(R.drawable.settings_switch_on);
					CurrentSetting.nopic = 1;
					sDao.update1(1);
				} else {
					switch_no_pic
							.setBackgroundResource(R.drawable.settings_switch_off);
					CurrentSetting.nopic = 0;
					sDao.update1(0);
				}
				break;
			case R.id.switch_wifi_only:
				if (CurrentSetting.onlywifi == 0) {
					switch_wifi_only
							.setBackgroundResource(R.drawable.settings_switch_on);
					CurrentSetting.onlywifi = 1;
					sDao.update2(1);
				} else {
					switch_wifi_only
							.setBackgroundResource(R.drawable.settings_switch_off);
					CurrentSetting.onlywifi = 0;
					sDao.update2(0);
				}
				break;
//			case R.id.switch_zero_flow:
//				if (CurrentSetting.zeroflow == 0) {
//					switch_zero_flow
//							.setBackgroundResource(R.drawable.settings_switch_on);
//					CurrentSetting.zeroflow = 1;
//					sDao.update3(1);
//				} else {
//					switch_zero_flow
//							.setBackgroundResource(R.drawable.settings_switch_off);
//					CurrentSetting.zeroflow = 0;
//					sDao.update3(0);
//				}
//				break;
			case R.id.switch_install_after_downloaded:
				if (CurrentSetting.autoinstall == 0) {
					switch_install_after_downloaded
							.setBackgroundResource(R.drawable.settings_switch_on);
					CurrentSetting.autoinstall = 1;
					sDao.update4(1);
				} else {
					switch_install_after_downloaded
							.setBackgroundResource(R.drawable.settings_switch_off);
					CurrentSetting.autoinstall = 0;
					sDao.update4(0);
				}
				break;
			case R.id.switch_delete_apk_after_downloaded:
				if (CurrentSetting.autodelete == 0) {
					switch_delete_apk_after_downloaded
							.setBackgroundResource(R.drawable.settings_switch_on);
					CurrentSetting.autodelete = 1;
					sDao.update5(1);
				} else {
					switch_delete_apk_after_downloaded
							.setBackgroundResource(R.drawable.settings_switch_off);
					CurrentSetting.autodelete = 0;
					sDao.update5(0);
				}
				break;
//			case R.id.switch_root_quick_install:
//				if (CurrentSetting.quickinstall == 0) {
//					switch_root_quick_install
//							.setBackgroundResource(R.drawable.settings_switch_on);
//					CurrentSetting.quickinstall = 1;
//					sDao.update6(1);
//				} else {
//					switch_root_quick_install
//							.setBackgroundResource(R.drawable.settings_switch_off);
//					CurrentSetting.quickinstall = 0;
//					sDao.update6(0);
//				}
//				break;
			case R.id.switch_app_update_notification:
				if (CurrentSetting.updatenotification == 0) {
					switch_app_update_notification
							.setBackgroundResource(R.drawable.settings_switch_on);
					CurrentSetting.updatenotification = 1;
					sDao.update7(1);
				} else {
					switch_app_update_notification
							.setBackgroundResource(R.drawable.settings_switch_off);
					CurrentSetting.updatenotification = 0;
					sDao.update7(0);
				}
				break;
//			case R.id.switch_screen_auto_rotation:
//				if (CurrentSetting.autorotation == 0) {
//					switch_screen_auto_rotation
//							.setBackgroundResource(R.drawable.settings_switch_on);
//					CurrentSetting.autorotation = 1;
//					sDao.update8(1);
//				} else {
//					switch_screen_auto_rotation
//							.setBackgroundResource(R.drawable.settings_switch_off);
//					CurrentSetting.autorotation = 0;
//					sDao.update8(0);
//				}
//				break;
			default:
				break;
			}
		}
	};
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
}
